﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NotebookS2.Inventory.Transactions
{
    public partial class IssuanceToProduction : Form
    {
        public IssuanceToProduction()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public DataTable LineItems { get; set; }

        private void IssuanceToProduction_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection oConnection = new SqlConnection();
                SqlCommand oCommand = new SqlCommand();

                oConnection.ConnectionString = this.ConnectionString;
                oCommand.Connection = oConnection;
                oCommand.CommandText = "INSERT INTO OISUPRD (DocNum, ReferenceMO, Remarks, DocStatus, PostingDate, DateCreated, CreatedBy, DateModified, ModifiedBy) VALUES (@DocNum, @ReferenceMO, @Remarks, @DocStatus, @PostingDate, @DateCreated, @CreatedBy, @DateModified, @ModifiedBy)";
                oCommand.Parameters.Add(new SqlParameter("@DocNum", txtDocNum.Text));
                oCommand.Parameters.Add(new SqlParameter("@ReferenceMO", txtMO.Text));
                oCommand.Parameters.Add(new SqlParameter("@Remarks", txtRemarks.Text));
                oCommand.Parameters.Add(new SqlParameter("@DocStatus", txtStatus.Text));
                oCommand.Parameters.Add(new SqlParameter("@PostingDate", dtPostingDate.EditValue));
                oCommand.Parameters.Add(new SqlParameter("@Datecreated", dtDateCreated.EditValue));
                oCommand.Parameters.Add(new SqlParameter("@CreatedBy", txtCreatedBy.Text));
                oCommand.Parameters.Add(new SqlParameter("@DateModified", dtDateModified.Text));
                oCommand.Parameters.Add(new SqlParameter("@ModifiedBy", txtModifiedBy.Text));

                oCommand.ExecuteNonQuery();


                foreach (DataRow oRow in this.LineItems.Rows)
                {
                    if (oRow.RowState != DataRowState.Deleted)
                    {
                        SqlCommand oLineCommand = new SqlCommand();
                        oLineCommand.Connection = oConnection;


                        oLineCommand.CommandText = "INSERT INTO MO1 (DocNum, ItemGroup, WhseCode, WHseName, ItemCode, Dscription, BaseQty, QtyRequirement, ActualQty,  UnitCost) VALUES (@DocNum, @ItemGroup, @WhseCode, @WHseName, @ItemCode, @Dscription, @BaseQty, @QtyRequirement, @ActualQty, @UnitCost)";

                        oLineCommand.Parameters.Add(new SqlParameter("@DocNum", txtDocNum.Text));
                        oLineCommand.Parameters.Add(new SqlParameter("@ItemGroup", oRow["ItemGroup"].ToString()));
                        oLineCommand.Parameters.Add(new SqlParameter("@WhseCode", oRow["WhseCode"].ToString()));
                        oLineCommand.Parameters.Add(new SqlParameter("@WhseName", oRow["WhseName"].ToString()));
                        oLineCommand.Parameters.Add(new SqlParameter("@ItemCode", oRow["ItemCode"].ToString()));
                        oLineCommand.Parameters.Add(new SqlParameter("@Dscription", oRow["Dscription"].ToString()));

                        oLineCommand.Parameters.Add(new SqlParameter("@BaseQty", oRow["BaseQty"].ToString()));
                        oLineCommand.Parameters.Add(new SqlParameter("@QtyRequirement", oRow["QtyRequirement"].ToString()));
                        oLineCommand.Parameters.Add(new SqlParameter("@ActualQty", oRow["ActualQty"].ToString()));
                        oLineCommand.Parameters.Add(new SqlParameter("@UnitCost", oRow["UnitCost"].ToString()));

                        oLineCommand.ExecuteNonQuery();
                    }
                }
                
            }
            catch
            {
            }
        }
    }
}
